package com.tallysystem.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tallysystem.dao.TallysDao;
import com.tallysystem.daoimpl.TallysDaoImpl;
import com.tallysystem.entity.Tallys;

@WebServlet("/addtallyServlet")
public class AddTallyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
		
		Double debit = Double.parseDouble(request.getParameter("debit"));
		String spendtype = request.getParameter("spendtype");
		String remarks = request.getParameter("remarks");
		//string 转成 date
		String spendtime01 = request.getParameter("spendtime");
		String clock01 = request.getParameter("clock");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = sdf.parse(spendtime01);
			date = sdf.parse(clock01);
        } catch (Exception e) {
            e.printStackTrace();

        }
		java.sql.Date spendtime = new java.sql.Date(date.getTime());
		java.sql.Date clock = new java.sql.Date(date.getTime());
//		System.out.println(spendtime);
		

//		int tid = Integer.parseInt(request.getParameter("tid"));
		TallysDao tallysDao = new TallysDaoImpl();
		tallysDao.addtallys(new Tallys(debit,spendtype,remarks,spendtime,clock,null,null,null));
		resp.sendRedirect("showTallys");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		super.doPost(req, resp);
	}
}
